﻿using System.Windows;
using System.Windows.Controls;
using Brjnk.Common;
using Brjnk.GpxExplorer.UI.ValidationRules;
using Brjnk.GpxExplorer.Workspace.Searching;

namespace Brjnk.GpxExplorer.UI
{
    /// <summary>
    /// Interaction logic for SearchPositionArgsEditor.xaml
    /// </summary>
    public partial class SearchPositionArgsEditor : UserControl
    {
        public SearchPositionArgsEditor()
        {
            InitializeComponent();
            this.LostFocus += new RoutedEventHandler(SearchPositionArgsEditor_LostFocus);

            mainGrd.DataContext = this;
            mainGrd.BindingGroup = new System.Windows.Data.BindingGroup();
            mainGrd.BindingGroup.ValidationRules.Add(new SearchPositionArgsEditorVr());
        }

        private void SearchPositionArgsEditor_LostFocus(object sender, RoutedEventArgs e)
        {
            mainGrd.BindingGroup.CommitEdit();
            LimitValidationFailed = (Validation.GetHasError(mainGrd));
        }

        public Distance Limit
        {
            get { return (Distance)GetValue(LimitProperty); }
            set { SetValue(LimitProperty, value); }
        }

        public static readonly DependencyProperty LimitProperty =
            DependencyProperty.Register("Limit", typeof(Distance), typeof(SearchPositionArgsEditor), new UIPropertyMetadata(new Distance(SearchService.DefaultLimit), Changed));

        public Distance SearchAgainLimit
        {
            get { return (Distance)GetValue(SearchAgainLimitProperty); }
            set { SetValue(SearchAgainLimitProperty, value); }
        }

        public static readonly DependencyProperty SearchAgainLimitProperty =
            DependencyProperty.Register("SearchAgainLimit", typeof(Distance), typeof(SearchPositionArgsEditor), new UIPropertyMetadata(new Distance(SearchService.DefaultSearchAgainLimit), Changed));

        public bool SearchInGaps
        {
            get { return (bool)GetValue(SearchInGapsProperty); }
            set { SetValue(SearchInGapsProperty, value); }
        }

        public static readonly DependencyProperty SearchInGapsProperty =
            DependencyProperty.Register("SearchInGaps", typeof(bool), typeof(SearchPositionArgsEditor), new UIPropertyMetadata(SearchService.DefaultSearchInGaps));

        public bool LimitValidationFailed
        {
            get { return (bool)GetValue(LimitValidationFailedProperty); }
            protected set { SetValue(LimitValidationFailedPropertyKey, value); }
        }

        private static readonly DependencyPropertyKey LimitValidationFailedPropertyKey =
            DependencyProperty.RegisterReadOnly("LimitValidationFailed", typeof(bool), typeof(SearchPositionArgsEditor), new PropertyMetadata(false));

        public static readonly DependencyProperty LimitValidationFailedProperty = LimitValidationFailedPropertyKey.DependencyProperty;

        private static void Changed(object sender, DependencyPropertyChangedEventArgs e)
        {
        }
    }
}